<?php

class AlbumBaseController extends AdminController {
	public $modelName = 'ObjectAlbum';
	public $oid;
	public $pid;
	
	public function init() {
		$this->oid = intval(isset($_GET['object_id']) ? $_GET['object_id'] : null);
		$this->pid = intval(isset($_GET['parent_id']) ? $_GET['parent_id'] : null);
		parent::init();
	}
	
	public function outerActions() {
		return array_merge(parent::outerActions(), array(
			'admin' => 'actions.album.admin',	
			'edit' => 'actions.album.edit',
		));
	}
	
	public function modelProvider() {
		
		$condition = "object_id = :oid AND parent_id = :pid";
		$params = array(':oid' => $this->oid, ':pid'=>$this->pid);
		
		return array_merge(parent::modelProvider(), array(
			'criteria' => array(
				'condition' => $condition,
				'params' => $params,
			),
		));
	}
	
	public function createUrl($route, $params = array(), $ampersand = '&') {
		$nodes = explode('/', $route);
		$default = false;
		$len = strlen($this->id);
		if(count($nodes) == 1) {
			$default = true;
		} else {
			$node1 = array_pop($nodes);
			$node2 = array_pop($nodes);
			$default = strncasecmp($node1, $this->id, $len) === 0;
			if($default) {
				$route .= '/admin';
			}
			$default |= strncasecmp($node2, $this->id, $len) === 0;
		}
		
		if($default) {
			foreach(array('parent_id'=>$this->pid, 'object_id'=>$this->oid) as $k=>$v) {
				if(!isset($params[$k])) {
					$params[$k] = $v;
				}
			}
		}
		return parent::createUrl($route, $params, $ampersand);
	}
}

?>